package com.lzs.bootsafety.business.sys.controller;

import com.alibaba.fastjson.JSON;
import com.github.pagehelper.PageInfo;
import com.lzs.bootsafety.business.sys.entity.SysDict;
import com.lzs.bootsafety.business.sys.service.SysDictService;
import com.lzs.bootsafety.business.sys.util.DictUtil;
import com.lzs.bootsafety.common.base.controller.BaseController;
import com.lzs.bootsafety.common.util.Resu;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.Date;
import java.util.HashMap;
import java.util.List;

@RestController
@RequestMapping("/sys/dict")
public class DictController extends BaseController {

    @Autowired
    private SysDictService sysDictService;
    @GetMapping("/getDictByType")
    public Resu getDictByType(@RequestParam("dictType") String dictType){
        List<SysDict> sysDicts = sysDictService.getSysDict(dictType);
        return Resu.suces().put("data",sysDicts);
    }
    @GetMapping("/getDictValue")
    public Resu getDictValue(String dictLabel,String dictType){
        return Resu.suces(DictUtil.getDictValue(dictLabel,dictType));
    }
    @GetMapping("/getDict")
    public String getDict(String dictLabel,String dictType){
        return DictUtil.getDictValue(dictLabel,dictType);
    }
    @GetMapping("/getDictList")
    public HashMap<String, String> DictList(){
        return DictUtil.hashMap;
    }
    @GetMapping("/list")
    @RequiresPermissions("sys:dict:list")
    public Resu list(@RequestParam("data")String data){
        SysDict sysDict = JSON.parseObject(data,SysDict.class);
        PageInfo<SysDict> sysDictPageInfo = sysDictService.getDictList(sysDict);
        return Resu.page(sysDictPageInfo.getList(),sysDictPageInfo.getTotal(),200);
    }
    @GetMapping("/info")
    @RequiresPermissions("sys:dict:info")
    public Resu info(long dictId){
        SysDict sysDict = sysDictService.getByPrimaryKey(dictId);
        return Resu.suces().put("data",sysDict);
    }
    @PostMapping("/add")
    @RequiresPermissions("sys:dict:add")
    public Resu add(@RequestBody SysDict sysDict){
        sysDict.setCreatePeople(getUserName());
        sysDict.setCreateTime(new Date());
        return respAdd(sysDictService.addSelective(sysDict));
    }
    @PutMapping("/modify")
    @RequiresPermissions("sys:dict:modify")
    public Resu modify(@RequestBody SysDict sysDict){
        sysDict.setUpdatePeople(getUserName());
        sysDict.setUpdateTime(new Date());
        return respudp(sysDictService.modifyByPrimaryKeySelective(sysDict));
    }
    @DeleteMapping("/del")
    @RequiresPermissions("sys:dict:del")
    public Resu del( Long[] dictIds){
        return respDel(sysDictService.delByPrimaryKey(dictIds));
    }
}
